package com.xquant.databasemanagement.bean.table;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.xquant.databasemanagement.enums.ColumnType;
import com.xquant.databasemanagement.enums.ColumnTypeMySQL;
import javafx.beans.property.SimpleStringProperty;
import org.apache.commons.lang.StringUtils;

import java.util.Objects;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ColumnBean {
    private final SimpleStringProperty columnName = new SimpleStringProperty();
    private final SimpleStringProperty columnLength = new SimpleStringProperty();
    private final SimpleStringProperty columnPrecision = new SimpleStringProperty();
    private final SimpleStringProperty columnAnnotation = new SimpleStringProperty();
    private final SimpleStringProperty columnType = new SimpleStringProperty();
    private final SimpleStringProperty nullable = new SimpleStringProperty();
    private final SimpleStringProperty columnDefault = new SimpleStringProperty();


    @JacksonXmlProperty(isAttribute = true)
    public String getColumnDefault() {
        return columnDefault.get();
    }

    public SimpleStringProperty columnDefaultProperty() {
        return columnDefault;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnDefault(String columnDefault) {
        this.columnDefault.set(columnDefault);
    }

    @JacksonXmlProperty(isAttribute = true)
    public boolean getNullable() {

        return Boolean.parseBoolean(nullable.get());
    }

    public SimpleStringProperty nullableProperty() {
        return nullable;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setNullable(String nullable) {
        this.nullable.set(nullable);
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getColumnName() {
        return columnName.get();
    }

    public SimpleStringProperty columnNameProperty() {
        return columnName;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnName(String columnName) {
        this.columnName.set(columnName);
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getColumnLength() {
        return columnLength.get();
    }

    public SimpleStringProperty columnLengthProperty() {
        return columnLength;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnLength(String columnLength) {
        this.columnLength.set(columnLength);
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getColumnPrecision() {
        return columnPrecision.get();
    }

    public SimpleStringProperty columnPrecisionProperty() {
        return columnPrecision;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnPrecision(String columnPrecision) {
        this.columnPrecision.set(columnPrecision);
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getColumnAnnotation() {
        return columnAnnotation.get();
    }

    public SimpleStringProperty columnAnnotationProperty() {
        return columnAnnotation;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnAnnotation(String columnAnnotation) {
        this.columnAnnotation.set(columnAnnotation);
    }

    @JacksonXmlProperty(isAttribute = true)
    public String getColumnType() {
        return columnType.get();
    }

    public SimpleStringProperty columnTypeProperty() {
        return columnType;
    }

    @JacksonXmlProperty(isAttribute = true)
    public void setColumnType(String columnType) {
        this.columnType.set(columnType);
    }

    public String columnOracleType(){
        return ColumnType.valueOf(columnType.get()).getValue();
    }

    public String columnMySQLType(){
        return ColumnTypeMySQL.valueOf(columnType.get()).getValue();
    }



    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (obj instanceof ColumnBean){
            ColumnBean columnBean = (ColumnBean) obj;
            return StringUtils.equals(this.getColumnName(),columnBean.getColumnName()) &&
                    StringUtils.equals(this.getColumnAnnotation(),columnBean.getColumnAnnotation())&&
                    StringUtils.equals(this.getColumnDefault(),columnBean.getColumnDefault())&&
                    StringUtils.equals(this.getColumnLength(),columnBean.getColumnLength())&&
                    StringUtils.equals(this.getColumnPrecision(),columnBean.getColumnPrecision())&&
                    (this.getNullable()==columnBean.getNullable())&&
                    StringUtils.equals(this.getColumnType(),columnBean.getColumnType());
        }
        return false;
    }

    @Override
    public String toString() {
        return columnName.get();
    }
}
